package com.emagic.manage.modules.room.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.format.Time;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.emagic.manage.LifeApplication;
import com.emagic.manage.R;
import com.emagic.manage.R2;
import com.emagic.manage.common.ui.base.ToolBarActivity;
import com.emagic.manage.modules.room.adapter.CommentListAdapter;
import com.emagic.manage.modules.room.view.AllRoomUser;
import com.emagic.manage.modules.room.view.ClassRoomManager;
import com.emagic.manage.modules.room.view.ClassRoomUser;
import com.emagic.manage.modules.room.view.IClassRoomManagerCbk;
import com.emagic.manage.modules.room.view.RoomFile;
import com.emagic.manage.utils.DisplayUtil;
import com.emagic.manage.widget.StatusBarCompat;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.xitaiinfo.library.commons.rx.Rx;
import com.xitaiinfo.library.utils.ViewUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.webrtc.EglBase;
import org.webrtc.SurfaceViewRenderer;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 学生课堂
 * Created by xiatian on 2017/11/1.
 */

public class RoomActivity extends ToolBarActivity implements IClassRoomManagerCbk {
    private static String TAG = "RoomActivity";
    private static final String SERIAL = "serial";             //课堂标示
    private static final String CHAIRMANPWD = "chairmanpwd";   //教师密码
    private static final String CONFUSERPWD = "confuserpwd";   //学生密码
    private static final String CLASSNAME = "classname";
    private static final String ROOMTYPE = "roomtype";   //教师类型
    private static final String USERID = "userId";//用户名
    private String watchingWhom = null;

    @BindView(R2.id.toolbar_layout)
    RelativeLayout mToolBarLayout;
    @BindView(R2.id.back_btn)
    TextView mBackBtn;
    @BindView(R2.id.room_name)
    TextView mRoomName;
    @BindView(R2.id.up_head_btn)
    TextView mUpHeadBtn;
    @BindView(R2.id.time_text)
    Chronometer mTimeText;

    @BindView(R2.id.left_layout)
    LinearLayout mLeftLayout;
    @BindView(R2.id.web_layout)
    RelativeLayout mWebLayout;
    @BindView(R2.id.show_view)
    WebView mShowView;
    @BindView(R2.id.surface_layout)
    LinearLayout mSurfaceLayout;
    @BindView(R2.id.surface_layout1)
    RelativeLayout mSurfaceLayout1;
    @BindView(R2.id.surfaceView1)
    SurfaceViewRenderer mSurfaceView1;
    @BindView(R2.id.bottom_layout1)
    LinearLayout mBottomLayout1;
    @BindView(R2.id.other_name1)
    TextView mOtherName1;
    @BindView(R2.id.video_img1)
    ImageView mVideoImg1;
    @BindView(R2.id.voice_img1)
    ImageView mVoiceImg1;
    @BindView(R2.id.draw_img1)
    ImageView mDrawImg1;
    @BindView(R2.id.surface_layout2)
    RelativeLayout mSurfaceLayout2;
    @BindView(R2.id.surfaceView2)
    SurfaceViewRenderer mSurfaceView2;
    @BindView(R2.id.bottom_layout2)
    LinearLayout mBottomLayout2;
    @BindView(R2.id.other_name2)
    TextView mOtherName2;
    @BindView(R2.id.video_img2)
    ImageView mVideoImg2;
    @BindView(R2.id.voice_img2)
    ImageView mVoiceImg2;
    @BindView(R2.id.draw_img2)
    ImageView mDrawImg2;
    @BindView(R2.id.surface_layout3)
    RelativeLayout mSurfaceLayout3;
    @BindView(R2.id.surfaceView3)
    SurfaceViewRenderer mSurfaceView3;
    @BindView(R2.id.bottom_layout3)
    LinearLayout mBottomLayout3;
    @BindView(R2.id.other_name3)
    TextView mOtherName3;
    @BindView(R2.id.video_img3)
    ImageView mVideoImg3;
    @BindView(R2.id.voice_img3)
    ImageView mVoiceImg3;
    @BindView(R2.id.draw_img3)
    ImageView mDrawImg3;
    @BindView(R2.id.surface_layout4)
    RelativeLayout mSurfaceLayout4;
    @BindView(R2.id.surfaceView4)
    SurfaceViewRenderer mSurfaceView4;
    @BindView(R2.id.bottom_layout4)
    LinearLayout mBottomLayout4;
    @BindView(R2.id.other_name4)
    TextView mOtherName4;
    @BindView(R2.id.video_img4)
    ImageView mVideoImg4;
    @BindView(R2.id.voice_img4)
    ImageView mVoiceImg4;
    @BindView(R2.id.draw_img4)
    ImageView mDrawImg4;
    @BindView(R2.id.surface_layout5)
    RelativeLayout mSurfaceLayout5;
    @BindView(R2.id.surfaceView5)
    SurfaceViewRenderer mSurfaceView5;
    @BindView(R2.id.bottom_layout5)
    LinearLayout mBottomLayout5;
    @BindView(R2.id.other_name5)
    TextView mOtherName5;
    @BindView(R2.id.video_img5)
    ImageView mVideoImg5;
    @BindView(R2.id.voice_img5)
    ImageView mVoiceImg5;
    @BindView(R2.id.draw_img5)
    ImageView mDrawImg5;
    @BindView(R2.id.surface_layout6)
    RelativeLayout mSurfaceLayout6;
    @BindView(R2.id.surfaceView6)
    SurfaceViewRenderer mSurfaceView6;
    @BindView(R2.id.bottom_layout6)
    LinearLayout mBottomLayout6;
    @BindView(R2.id.other_name6)
    TextView mOtherName6;
    @BindView(R2.id.video_img6)
    ImageView mVideoImg6;
    @BindView(R2.id.voice_img6)
    ImageView mVoiceImg6;
    @BindView(R2.id.draw_img6)
    ImageView mDrawImg6;

    @BindView(R2.id.right_layout)
    LinearLayout mRightLayout;
    @BindView(R2.id.teacher_layout)
    LinearLayout mTeacherLayout;
    @BindView(R2.id.surfaceViewTeacher)
    SurfaceViewRenderer mSurfaceViewTeacher;
    @BindView(R2.id.teacher_name)
    TextView mTeacherName;
    @BindView(R2.id.my_layout)
    LinearLayout mMyLayout;
    @BindView(R2.id.surfaceViewMine)
    SurfaceViewRenderer mSurfaceViewMine;
    @BindView(R2.id.gift_num)
    TextView mGiftNum;
    @BindView(R2.id.user_name)
    TextView mUserName;
    @BindView(R2.id.ren_img)
    ImageView mRenImg;
    @BindView(R2.id.say_img)
    ImageView mSayImg;
    @BindView(R2.id.draw_img)
    ImageView mDrawImg;
    @BindView(R2.id.comm_layout)
    LinearLayout mCommLayout;
    @BindView(R2.id.comm_list)
    RecyclerView mCommList;
    @BindView(R2.id.say_edt)
    EditText mSayEdt;
    @BindView(R2.id.push_btn)
    TextView mPushBtn;

    @BindView(R2.id.answer_layout)
    RelativeLayout mAnswerLayout;
    @BindView(R2.id.time_show)
    TextView mTimeShow;
    @BindView(R2.id.time_btn)
    TextView mTimeBtn;
    @BindView(R2.id.winner_text)
    TextView mWinner;

    @BindView(R2.id.play_dice1)
    ImageView mPlayImg1;
    @BindView(R2.id.play_dice2)
    ImageView mPlayImg2;
    @BindView(R2.id.play_dice3)
    ImageView mPlayImg3;
    @BindView(R2.id.play_dice4)
    ImageView mPlayImg4;
    @BindView(R2.id.play_dice5)
    ImageView mPlayImg5;
    @BindView(R2.id.play_dice6)
    ImageView mPlayImg6;

    @BindView(R2.id.video_show_layout)
    LinearLayout mVideoShowLayout;
    @BindView(R2.id.video_view)
    VideoView mVideoView;
    @BindView(R2.id.time_change_text)
    TextView mTimeChangeTV;
    @BindView(R2.id.seekBar_volume)
    SeekBar mSeekBarVolume;
    @BindView(R2.id.seek_bar)
    SeekBar mSeekBar;
    @BindView(R2.id.audio_name)
    TextView mAudioName;
    @BindView(R2.id.enlarge_btn)
    ImageView mEnlargeBtn;

    private String serial, chairmanpwd, confuserpwd, classname, teacherId, roomType;

    private CommentListAdapter mAdapter;

    private boolean isPlaying = false;
    private boolean isStop = true;

    private int leftViewHeight, leftViewWidth, rightViewHeight, rightViewWidth;

    private boolean isBackPlay = false;
    private AudioManager audioManager;
    private boolean isClassBegin = false;
    private boolean needJoin = false;
    private long classStarTime = 0, seveiceTime = 0, localTime = 0;
    private String type = null;
    private String filename;

    private StringBuilder mFormatBuilder;
    private Formatter mFormatter;

    private List<AllRoomUser.RoomUser> allRoomUserList = new ArrayList<>();

    private List<AllRoomUser.RoomUser> recUserList = new ArrayList<>();

    private List<String> bottomViews = new ArrayList<>();

    private ProgressDialog progressDialog = null;
    private MediaPlayer player = new MediaPlayer();

    private int maxVolume, syscurrenvolume;
    private String oldID = "";

    public static Intent getCallingIntent(Context context, String serial, String chairmanpwd,
                                          String confuserpwd, String classname, String roomtype) {

        Intent intent = new Intent(context, RoomActivity.class);
        intent.putExtra(SERIAL, serial);
        intent.putExtra(CHAIRMANPWD, chairmanpwd);
        intent.putExtra(CONFUSERPWD, confuserpwd);
        intent.putExtra(CLASSNAME, classname);
        intent.putExtra(ROOMTYPE, roomtype);
        return intent;
    }

    public static void toAction(Context context,
                                String serial,
                                String confuserpwd,
                                String classname,
                                String roomtype) {

        Intent intent = new Intent(context, RoomActivity.class);
        intent.putExtra(SERIAL, serial);
        intent.putExtra(CONFUSERPWD, confuserpwd);
        intent.putExtra(CLASSNAME, classname);
        intent.putExtra(ROOMTYPE, roomtype);
        context.startActivity(intent);
    }

    public static void removeOnGlobalLayoutListener(View view, ViewTreeObserver.OnGlobalLayoutListener victim) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.getViewTreeObserver().removeOnGlobalLayoutListener(victim);
        } else {
            view.getViewTreeObserver().removeGlobalOnLayoutListener(victim);
        }
    }

    private void initView() {
        mSurfaceViewMine.init(EglBase.create().getEglBaseContext(), null);
        mFormatBuilder = new StringBuilder();
        mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());

        if (roomType.equals("0")) {
            mUpHeadBtn.setVisibility(View.INVISIBLE);
            mUpHeadBtn.setEnabled(false);
            mSurfaceLayout.setVisibility(View.GONE);
        } else {
            mUpHeadBtn.setVisibility(View.VISIBLE);
            mUpHeadBtn.setEnabled(true);
            mSurfaceLayout.setVisibility(View.VISIBLE);
        }

        //进度条绑定最大音量，最大音量
        mSeekBarVolume.setMax(maxVolume);
        //进度条绑定当前音量
        mSeekBarVolume.setProgress(syscurrenvolume);
        mSeekBarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //取得当前进度
                int tmpInt = seekBar.getProgress();

                // 当进度小于1时，设置成1，防止太小。
                if (tmpInt < 1) {
                    tmpInt = 1;
                }

                // 根据当前进度改变亮度
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, tmpInt, 0);
            }
        });


        int screenWidth = DisplayUtil.getScreenWidth(this);
        int screenHeight = DisplayUtil.getScreenHeight(this);
        double screenH2W = (double) screenHeight / (double) screenWidth;
        ViewTreeObserver viewTreeObserver = mTeacherLayout.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                removeOnGlobalLayoutListener(mTeacherLayout, this);
                rightViewWidth = mTeacherLayout.getWidth();
                rightViewHeight =  mTeacherLayout.getHeight();
//                rightViewHeight = (int) (rightViewWidth * screenH2W);
//                mTeacherLayout.setLayoutParams(new LinearLayout.LayoutParams(rightViewWidth, rightViewHeight));
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(rightViewWidth, rightViewHeight);
                int margin = ViewUtils.dip2px(RoomActivity.this, 10);
//                layoutParams.setMargins(margin, margin, margin, 0);
                layoutParams.setMargins(0, 0, 0, 0);
                mMyLayout.setLayoutParams(layoutParams);
            }
        });


        ViewTreeObserver left = mSurfaceView1.getViewTreeObserver();
        left.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                removeOnGlobalLayoutListener(mSurfaceView1, this);
                leftViewHeight = mSurfaceView1.getHeight();
                leftViewWidth = mSurfaceView1.getWidth();
                System.out.println("--->" + leftViewHeight + "*" + leftViewWidth);
            }
        });


        showLoadDialog();
        mShowView.getSettings().setJavaScriptEnabled(true);
        mShowView.addJavascriptInterface(new AndroidBridge(), "JSWhitePadInterface"); //将那个实例化的函数类设置为”android"的js接口。
        //启用数据
        mShowView.getSettings().setDatabaseEnabled(true);
        //启用地理定位
        mShowView.getSettings().setGeolocationEnabled(true);
        //开启DomStorage缓存
        mShowView.getSettings().setDomStorageEnabled(true);
        mShowView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mShowView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mShowView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mShowView.getSettings().setDefaultTextEncodingName("utf_8");
        mShowView.loadUrl("https://www.weiyiclass.com/static/pad_demo/index.html");
        mShowView.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();// 接受所有网站的证书
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                view.loadUrl("javascript:GLOBAL.phone.setInitPageParameterFormPhone({\n" +
                        "        \"roomrole\" : 2, \n" +
                        "        \"mClientType\" : 3,\n" +
                        "        \"roomtype\" : " + roomType + ",\n" +
                        "        \"serviceUrl\" : {\n" +
                        "            \"address\" : \"https://www.weiyiclass.com\",\n" +
                        "            \"port\" : 443\n" +
                        "        }\n" +
                        "    });");

                view.loadUrl("javascript:GLOBAL.phone.drawPermission(false)");

                afterPageFinished();

            }
        });

    }

    private void afterPageFinished() {
        RequestParams param = new RequestParams();
        param.put("serial", serial);
        param.put("userid", LifeApplication.getInstance().getCurrentUser().getUserid());
        param.put("userrole", LifeApplication.getInstance().getCurrentUser().getUserroleid());


        param.put("password", confuserpwd);


        Log.d("param", param.toString());

        Log.d(TAG, "Start!");
        ClassRoomManager.globalInitialize(getContext());
        ClassRoomManager.getInstance().setCallbBack(this);
        ClassRoomManager.getInstance().joinRoom("media01.weiyiclass.com",
//                "edu.weiyicloud.com", "8443", "443",//原“好学成”用的ip
                "www.weiyiclass.com", "8443", "443",
                LifeApplication.getInstance().getCurrentUser().getFirstname(),
                param, mSurfaceViewMine, true, "student");

    }

    private void showLoadDialog() {
        if (null == progressDialog) {
            progressDialog = ProgressDialog.show(RoomActivity.this, "", "正在初始化，请稍候...");
            progressDialog.setCancelable(false);
        } else {
            progressDialog.show();
        }
    }

    private void dismissLoadDialog() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (null != progressDialog && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.colorRoomState));
        getToolbar().setVisibility(View.GONE);
        serial = getIntent().getStringExtra(SERIAL);
        chairmanpwd = getIntent().getStringExtra(CHAIRMANPWD);
        confuserpwd = getIntent().getStringExtra(CONFUSERPWD);
        classname = getIntent().getStringExtra(CLASSNAME);
        roomType = getIntent().getStringExtra(ROOMTYPE);
        ;

        //获取音频服务
        audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        //设置声音模式
        audioManager.setMode(AudioManager.STREAM_MUSIC);
        //取得最大音量
        maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        //取得当前音量
        syscurrenvolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        initView();
        initAction();

    }

    @Override
    protected void onDestroy() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        if (player != null) {
            player.stop();
        }
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
        super.onDestroy();
    }

    private void initAction() {
        Rx.click(mBackBtn, 1000, v -> {
            if (!isClassBegin) {
                ClassRoomManager.getInstance().set_status(0);
                watchingWhom = null;
                this.finish();
            } else {
                watchingWhom = null;
                showLeaveDialog();
            }
        });

        mRoomName.setText(classname);

        Rx.click(mUpHeadBtn, 1000, v -> {//举手
            if (isClassBegin) {
                ClassRoomManager.getInstance().changeUserProperty(ClassRoomManager.getInstance().getMySelf().peerId,
                        "__all", "raisehand", true);
            } else {
                Toast.makeText(this, "课程还没有开始", Toast.LENGTH_SHORT).show();
            }
        });

        Rx.click(mPushBtn, 1000, v -> {//发言

            if (!TextUtils.isEmpty(mSayEdt.getText().toString().trim())) {
                ClassRoomManager.getInstance().sendMessage(mSayEdt.getText().toString().trim());
            } else {
                Toast.makeText(this, "发送内容不能为空", Toast.LENGTH_SHORT).show();
            }

        });

        Rx.click(mEnlargeBtn, 1000, v -> {
            if (!isBackPlay) {
                isBackPlay = true;
                mToolBarLayout.setVisibility(View.GONE);
                mRightLayout.setVisibility(View.GONE);
                mSurfaceViewTeacher.setVisibility(View.GONE);
                mSurfaceViewMine.setVisibility(View.GONE);
                mSurfaceLayout.setVisibility(View.GONE);
                mSurfaceView1.setVisibility(View.GONE);
                mSurfaceView2.setVisibility(View.GONE);
                mSurfaceView3.setVisibility(View.GONE);
                mSurfaceView4.setVisibility(View.GONE);
                mSurfaceView5.setVisibility(View.GONE);
                mSurfaceView6.setVisibility(View.GONE);
            } else {
                isBackPlay = false;
                mToolBarLayout.setVisibility(View.VISIBLE);
                mRightLayout.setVisibility(View.VISIBLE);
                mTeacherLayout.setVisibility(View.VISIBLE);
                mSurfaceViewTeacher.setVisibility(View.VISIBLE);
                mSurfaceViewMine.setVisibility(View.VISIBLE);
                mSurfaceLayout.setVisibility(View.VISIBLE);
                mSurfaceView1.setVisibility(View.VISIBLE);
                mSurfaceView2.setVisibility(View.VISIBLE);
                mSurfaceView3.setVisibility(View.VISIBLE);
                mSurfaceView4.setVisibility(View.VISIBLE);
                mSurfaceView5.setVisibility(View.VISIBLE);
                mSurfaceView6.setVisibility(View.VISIBLE);
            }
        });

        Rx.click(mSurfaceViewMine, 1000, v -> {

            if (!isBackPlay) {
                isBackPlay = true;
                mToolBarLayout.setVisibility(View.GONE);
                mLeftLayout.setVisibility(View.GONE);
                mTeacherLayout.setVisibility(View.GONE);
                mCommLayout.setVisibility(View.GONE);
                mSurfaceViewTeacher.setVisibility(View.GONE);
                mSurfaceView1.setVisibility(View.GONE);
                mSurfaceView2.setVisibility(View.GONE);
                mSurfaceView3.setVisibility(View.GONE);
                mSurfaceView4.setVisibility(View.GONE);
                mSurfaceView5.setVisibility(View.GONE);
                mSurfaceView6.setVisibility(View.GONE);
                LinearLayout.LayoutParams myLayout = (LinearLayout.LayoutParams) mMyLayout.getLayoutParams();
                myLayout.height = ViewGroup.LayoutParams.MATCH_PARENT;
                myLayout.width = ViewGroup.LayoutParams.MATCH_PARENT;
                myLayout.setMargins(0, 0, 0, 0);
                mMyLayout.setLayoutParams(myLayout);

            } else {

                mToolBarLayout.setVisibility(View.VISIBLE);
//                mLeftLayout.setVisibility(View.VISIBLE);
                mLeftLayout.setVisibility(View.GONE);
                mTeacherLayout.setVisibility(View.VISIBLE);
                mCommLayout.setVisibility(View.VISIBLE);
                mSurfaceViewTeacher.setVisibility(View.VISIBLE);
                mSurfaceView1.setVisibility(View.VISIBLE);
                mSurfaceView2.setVisibility(View.VISIBLE);
                mSurfaceView3.setVisibility(View.VISIBLE);
                mSurfaceView4.setVisibility(View.VISIBLE);
                mSurfaceView5.setVisibility(View.VISIBLE);
                mSurfaceView6.setVisibility(View.VISIBLE);

                isBackPlay = false;
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mMyLayout.getLayoutParams();
                lp.height = rightViewHeight;
                lp.width = rightViewWidth;
                int margin = ViewUtils.dip2px(RoomActivity.this, 10);
//                lp.setMargins(margin, margin, margin, 0);
//                lp.setMargins(0, 0, 0, 0);
//                mMyLayout.setLayoutParams(lp);


            }

        });

        Rx.click(mSurfaceViewTeacher, 1000, v -> {
            if (!isBackPlay) {
                mToolBarLayout.setVisibility(View.GONE);
                mLeftLayout.setVisibility(View.GONE);
                mMyLayout.setVisibility(View.GONE);
                mCommLayout.setVisibility(View.GONE);
                mSurfaceViewMine.setVisibility(View.GONE);
                mSurfaceView1.setVisibility(View.GONE);
                mSurfaceView2.setVisibility(View.GONE);
                mSurfaceView3.setVisibility(View.GONE);
                mSurfaceView4.setVisibility(View.GONE);
                mSurfaceView5.setVisibility(View.GONE);
                mSurfaceView6.setVisibility(View.GONE);

                isBackPlay = true;
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTeacherLayout.getLayoutParams();
                lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.setMargins(0, 0, 0, 0);
                mTeacherLayout.setLayoutParams(lp);


            } else {

                mToolBarLayout.setVisibility(View.VISIBLE);
//                mLeftLayout.setVisibility(View.VISIBLE);
                mLeftLayout.setVisibility(View.GONE);
                mMyLayout.setVisibility(View.VISIBLE);
                mCommLayout.setVisibility(View.VISIBLE);
                mSurfaceViewMine.setVisibility(View.VISIBLE);
                mSurfaceView1.setVisibility(View.VISIBLE);
                mSurfaceView2.setVisibility(View.VISIBLE);
                mSurfaceView3.setVisibility(View.VISIBLE);
                mSurfaceView4.setVisibility(View.VISIBLE);
                mSurfaceView5.setVisibility(View.VISIBLE);
                mSurfaceView6.setVisibility(View.VISIBLE);

                isBackPlay = false;
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTeacherLayout.getLayoutParams();
                lp.height = rightViewHeight;
                lp.width = rightViewWidth;
                int margin = ViewUtils.dip2px(RoomActivity.this, 10);
//                lp.setMargins(margin, 0, margin, 0);
//                lp.setMargins(0, 0, 0, 0);
//                mTeacherLayout.setLayoutParams(lp);
            }
        });

        Rx.click(mSurfaceView1, 1000, v -> {
            if (mBottomLayout1.getVisibility() == View.VISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView1.getLayoutParams();
                lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                System.out.println(lp.width);
                mSurfaceView1.setLayoutParams(lp);

                mBottomLayout1.setVisibility(View.INVISIBLE);
                mToolBarLayout.setVisibility(View.GONE);
                mRightLayout.setVisibility(View.GONE);
                mWebLayout.setVisibility(View.GONE);
                mSurfaceLayout2.setVisibility(View.GONE);
                mSurfaceLayout3.setVisibility(View.GONE);
                mSurfaceLayout4.setVisibility(View.GONE);
                mSurfaceLayout5.setVisibility(View.GONE);
                mSurfaceLayout6.setVisibility(View.GONE);
                mSurfaceViewMine.setVisibility(View.GONE);
                mSurfaceViewTeacher.setVisibility(View.GONE);
                mSurfaceView2.setVisibility(View.GONE);
                mSurfaceView3.setVisibility(View.GONE);
                mSurfaceView4.setVisibility(View.GONE);
                mSurfaceView5.setVisibility(View.GONE);
                mSurfaceView6.setVisibility(View.GONE);

                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout1.getLayoutParams();
                surfaceLayout.setMargins(0, 0, 0, 0);
                mSurfaceLayout1.setLayoutParams(surfaceLayout);

            } else if (mBottomLayout1.getVisibility() == View.INVISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView1.getLayoutParams();
                lp.height = leftViewHeight;
                lp.width = leftViewWidth;
                System.out.println(lp.width);
                mSurfaceView1.setLayoutParams(lp);

                mBottomLayout1.setVisibility(View.VISIBLE);
                mToolBarLayout.setVisibility(View.VISIBLE);
                mRightLayout.setVisibility(View.VISIBLE);
                mWebLayout.setVisibility(View.VISIBLE);
                mSurfaceLayout2.setVisibility(View.VISIBLE);
                mSurfaceLayout3.setVisibility(View.VISIBLE);
                mSurfaceLayout4.setVisibility(View.VISIBLE);
                mSurfaceLayout5.setVisibility(View.VISIBLE);
                mSurfaceLayout6.setVisibility(View.VISIBLE);
                mSurfaceViewMine.setVisibility(View.VISIBLE);
                mSurfaceViewTeacher.setVisibility(View.VISIBLE);
                mSurfaceView2.setVisibility(View.VISIBLE);
                mSurfaceView3.setVisibility(View.VISIBLE);
                mSurfaceView4.setVisibility(View.VISIBLE);
                mSurfaceView5.setVisibility(View.VISIBLE);
                mSurfaceView6.setVisibility(View.VISIBLE);

                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout1.getLayoutParams();
                surfaceLayout.setMargins(0, 0, ViewUtils.dip2px(this, 10f), 0);
                mSurfaceLayout1.setLayoutParams(surfaceLayout);

            }
        });
        Rx.click(mSurfaceView2, 1000, v -> {
            if (mBottomLayout2.getVisibility() == View.VISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView2.getLayoutParams();
                lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                System.out.println(lp.width);
                mSurfaceView2.setLayoutParams(lp);

                mBottomLayout2.setVisibility(View.INVISIBLE);
                mToolBarLayout.setVisibility(View.GONE);
                mRightLayout.setVisibility(View.GONE);
                mWebLayout.setVisibility(View.GONE);
                mSurfaceLayout1.setVisibility(View.GONE);
                mSurfaceLayout3.setVisibility(View.GONE);
                mSurfaceLayout4.setVisibility(View.GONE);
                mSurfaceLayout5.setVisibility(View.GONE);
                mSurfaceLayout6.setVisibility(View.GONE);
                mSurfaceViewMine.setVisibility(View.GONE);
                mSurfaceViewTeacher.setVisibility(View.GONE);
                mSurfaceView1.setVisibility(View.GONE);
                mSurfaceView3.setVisibility(View.GONE);
                mSurfaceView4.setVisibility(View.GONE);
                mSurfaceView5.setVisibility(View.GONE);
                mSurfaceView6.setVisibility(View.GONE);

                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout2.getLayoutParams();
                surfaceLayout.setMargins(0, 0, 0, 0);
                mSurfaceLayout2.setLayoutParams(surfaceLayout);

            } else if (mBottomLayout2.getVisibility() == View.INVISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView2.getLayoutParams();
                lp.height = leftViewHeight;
                lp.width = leftViewWidth;
                System.out.println(lp.width);
                mSurfaceView2.setLayoutParams(lp);

                mBottomLayout2.setVisibility(View.VISIBLE);
                mToolBarLayout.setVisibility(View.VISIBLE);
                mRightLayout.setVisibility(View.VISIBLE);
                mWebLayout.setVisibility(View.VISIBLE);
                mSurfaceLayout1.setVisibility(View.VISIBLE);
                mSurfaceLayout3.setVisibility(View.VISIBLE);
                mSurfaceLayout4.setVisibility(View.VISIBLE);
                mSurfaceLayout5.setVisibility(View.VISIBLE);
                mSurfaceLayout6.setVisibility(View.VISIBLE);
                mSurfaceViewMine.setVisibility(View.VISIBLE);
                mSurfaceViewTeacher.setVisibility(View.VISIBLE);
                mSurfaceView1.setVisibility(View.VISIBLE);
                mSurfaceView3.setVisibility(View.VISIBLE);
                mSurfaceView4.setVisibility(View.VISIBLE);
                mSurfaceView5.setVisibility(View.VISIBLE);
                mSurfaceView6.setVisibility(View.VISIBLE);

                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout2.getLayoutParams();
                surfaceLayout.setMargins(0, 0, ViewUtils.dip2px(this, 10f), 0);
                mSurfaceLayout2.setLayoutParams(surfaceLayout);

            }
        });
        Rx.click(mSurfaceView3, 1000, v -> {
            if (mBottomLayout3.getVisibility() == View.VISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView3.getLayoutParams();
                lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                System.out.println(lp.width);
                mSurfaceView3.setLayoutParams(lp);

                mBottomLayout3.setVisibility(View.INVISIBLE);
                mToolBarLayout.setVisibility(View.GONE);
                mRightLayout.setVisibility(View.GONE);
                mWebLayout.setVisibility(View.GONE);
                mSurfaceLayout1.setVisibility(View.GONE);
                mSurfaceLayout2.setVisibility(View.GONE);
                mSurfaceLayout4.setVisibility(View.GONE);
                mSurfaceLayout5.setVisibility(View.GONE);
                mSurfaceLayout6.setVisibility(View.GONE);
                mSurfaceViewMine.setVisibility(View.GONE);
                mSurfaceViewTeacher.setVisibility(View.GONE);
                mSurfaceView1.setVisibility(View.GONE);
                mSurfaceView2.setVisibility(View.GONE);
                mSurfaceView4.setVisibility(View.GONE);
                mSurfaceView5.setVisibility(View.GONE);
                mSurfaceView6.setVisibility(View.GONE);

                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout3.getLayoutParams();
                surfaceLayout.setMargins(0, 0, 0, 0);
                mSurfaceLayout3.setLayoutParams(surfaceLayout);

            } else if (mBottomLayout3.getVisibility() == View.INVISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView3.getLayoutParams();
                lp.height = leftViewHeight;
                lp.width = leftViewWidth;
                System.out.println(lp.width);
                mSurfaceView3.setLayoutParams(lp);

                mBottomLayout3.setVisibility(View.VISIBLE);
                mToolBarLayout.setVisibility(View.VISIBLE);
                mRightLayout.setVisibility(View.VISIBLE);
                mWebLayout.setVisibility(View.VISIBLE);
                mSurfaceLayout1.setVisibility(View.VISIBLE);
                mSurfaceLayout2.setVisibility(View.VISIBLE);
                mSurfaceLayout4.setVisibility(View.VISIBLE);
                mSurfaceLayout5.setVisibility(View.VISIBLE);
                mSurfaceLayout6.setVisibility(View.VISIBLE);
                mSurfaceViewMine.setVisibility(View.VISIBLE);
                mSurfaceViewTeacher.setVisibility(View.VISIBLE);
                mSurfaceView1.setVisibility(View.VISIBLE);
                mSurfaceView2.setVisibility(View.VISIBLE);
                mSurfaceView4.setVisibility(View.VISIBLE);
                mSurfaceView5.setVisibility(View.VISIBLE);
                mSurfaceView6.setVisibility(View.VISIBLE);

                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout3.getLayoutParams();
                surfaceLayout.setMargins(0, 0, ViewUtils.dip2px(this, 10f), 0);
                mSurfaceLayout3.setLayoutParams(surfaceLayout);

            }
        });
        Rx.click(mSurfaceView4, 1000, v -> {
            if (mBottomLayout4.getVisibility() == View.VISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView4.getLayoutParams();
                lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                System.out.println(lp.width);
                mSurfaceView4.setLayoutParams(lp);

                mBottomLayout4.setVisibility(View.INVISIBLE);
                mToolBarLayout.setVisibility(View.GONE);
                mRightLayout.setVisibility(View.GONE);
                mWebLayout.setVisibility(View.GONE);
                mSurfaceLayout1.setVisibility(View.GONE);
                mSurfaceLayout2.setVisibility(View.GONE);
                mSurfaceLayout3.setVisibility(View.GONE);
                mSurfaceLayout5.setVisibility(View.GONE);
                mSurfaceLayout6.setVisibility(View.GONE);
                mSurfaceViewMine.setVisibility(View.GONE);
                mSurfaceViewTeacher.setVisibility(View.GONE);
                mSurfaceView1.setVisibility(View.GONE);
                mSurfaceView2.setVisibility(View.GONE);
                mSurfaceView3.setVisibility(View.GONE);
                mSurfaceView5.setVisibility(View.GONE);
                mSurfaceView6.setVisibility(View.GONE);

                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout4.getLayoutParams();
                surfaceLayout.setMargins(0, 0, 0, 0);
                mSurfaceLayout4.setLayoutParams(surfaceLayout);

            } else if (mBottomLayout4.getVisibility() == View.INVISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView4.getLayoutParams();
                lp.height = leftViewHeight;
                lp.width = leftViewWidth;
                System.out.println(lp.width);
                mSurfaceView4.setLayoutParams(lp);

                mBottomLayout4.setVisibility(View.VISIBLE);
                mToolBarLayout.setVisibility(View.VISIBLE);
                mRightLayout.setVisibility(View.VISIBLE);
                mWebLayout.setVisibility(View.VISIBLE);
                mSurfaceLayout1.setVisibility(View.VISIBLE);
                mSurfaceLayout2.setVisibility(View.VISIBLE);
                mSurfaceLayout3.setVisibility(View.VISIBLE);
                mSurfaceLayout5.setVisibility(View.VISIBLE);
                mSurfaceLayout6.setVisibility(View.VISIBLE);
                mSurfaceViewMine.setVisibility(View.VISIBLE);
                mSurfaceViewTeacher.setVisibility(View.VISIBLE);
                mSurfaceView1.setVisibility(View.VISIBLE);
                mSurfaceView2.setVisibility(View.VISIBLE);
                mSurfaceView3.setVisibility(View.VISIBLE);
                mSurfaceView5.setVisibility(View.VISIBLE);
                mSurfaceView6.setVisibility(View.VISIBLE);


                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout4.getLayoutParams();
                surfaceLayout.setMargins(0, 0, ViewUtils.dip2px(this, 10f), 0);
                mSurfaceLayout4.setLayoutParams(surfaceLayout);

            }
        });
        Rx.click(mSurfaceView5, 1000, v -> {
            if (mBottomLayout5.getVisibility() == View.VISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView5.getLayoutParams();
                lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                System.out.println(lp.width);
                mSurfaceView5.setLayoutParams(lp);

                mBottomLayout5.setVisibility(View.INVISIBLE);
                mToolBarLayout.setVisibility(View.GONE);
                mRightLayout.setVisibility(View.GONE);
                mWebLayout.setVisibility(View.GONE);
                mSurfaceLayout1.setVisibility(View.GONE);
                mSurfaceLayout2.setVisibility(View.GONE);
                mSurfaceLayout3.setVisibility(View.GONE);
                mSurfaceLayout4.setVisibility(View.GONE);
                mSurfaceLayout6.setVisibility(View.GONE);
                mSurfaceViewMine.setVisibility(View.GONE);
                mSurfaceViewTeacher.setVisibility(View.GONE);
                mSurfaceView1.setVisibility(View.GONE);
                mSurfaceView2.setVisibility(View.GONE);
                mSurfaceView3.setVisibility(View.GONE);
                mSurfaceView4.setVisibility(View.GONE);
                mSurfaceView6.setVisibility(View.GONE);


                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout5.getLayoutParams();
                surfaceLayout.setMargins(0, 0, 0, 0);
                mSurfaceLayout5.setLayoutParams(surfaceLayout);

            } else if (mBottomLayout5.getVisibility() == View.INVISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView5.getLayoutParams();
                lp.height = leftViewHeight;
                lp.width = leftViewWidth;
                System.out.println(lp.width);
                mSurfaceView5.setLayoutParams(lp);

                mBottomLayout5.setVisibility(View.VISIBLE);
                mToolBarLayout.setVisibility(View.VISIBLE);
                mRightLayout.setVisibility(View.VISIBLE);
                mWebLayout.setVisibility(View.VISIBLE);
                mSurfaceLayout1.setVisibility(View.VISIBLE);
                mSurfaceLayout2.setVisibility(View.VISIBLE);
                mSurfaceLayout3.setVisibility(View.VISIBLE);
                mSurfaceLayout4.setVisibility(View.VISIBLE);
                mSurfaceLayout6.setVisibility(View.VISIBLE);
                mSurfaceViewMine.setVisibility(View.VISIBLE);
                mSurfaceViewTeacher.setVisibility(View.VISIBLE);
                mSurfaceView1.setVisibility(View.VISIBLE);
                mSurfaceView2.setVisibility(View.VISIBLE);
                mSurfaceView3.setVisibility(View.VISIBLE);
                mSurfaceView4.setVisibility(View.VISIBLE);
                mSurfaceView6.setVisibility(View.VISIBLE);

                LinearLayout.LayoutParams surfaceLayout = (LinearLayout.LayoutParams) mSurfaceLayout5.getLayoutParams();
                surfaceLayout.setMargins(0, 0, ViewUtils.dip2px(this, 10f), 0);
                mSurfaceLayout5.setLayoutParams(surfaceLayout);

            }
        });
        Rx.click(mSurfaceView6, 1000, v -> {
            if (mBottomLayout6.getVisibility() == View.VISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView6.getLayoutParams();
                lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                System.out.println(lp.width);
                mSurfaceView6.setLayoutParams(lp);

                mBottomLayout6.setVisibility(View.INVISIBLE);
                mToolBarLayout.setVisibility(View.GONE);
                mRightLayout.setVisibility(View.GONE);
                mWebLayout.setVisibility(View.GONE);
                mSurfaceLayout1.setVisibility(View.GONE);
                mSurfaceLayout2.setVisibility(View.GONE);
                mSurfaceLayout3.setVisibility(View.GONE);
                mSurfaceLayout4.setVisibility(View.GONE);
                mSurfaceLayout5.setVisibility(View.GONE);
                mSurfaceViewMine.setVisibility(View.GONE);
                mSurfaceViewTeacher.setVisibility(View.GONE);
                mSurfaceView1.setVisibility(View.GONE);
                mSurfaceView2.setVisibility(View.GONE);
                mSurfaceView3.setVisibility(View.GONE);
                mSurfaceView4.setVisibility(View.GONE);
                mSurfaceView5.setVisibility(View.GONE);

            } else if (mBottomLayout6.getVisibility() == View.INVISIBLE) {
                ViewGroup.LayoutParams lp = mSurfaceView6.getLayoutParams();
                lp.height = leftViewHeight;
                lp.width = leftViewWidth;
                System.out.println(lp.width);
                mSurfaceView6.setLayoutParams(lp);

                mBottomLayout6.setVisibility(View.VISIBLE);
                mToolBarLayout.setVisibility(View.VISIBLE);
                mRightLayout.setVisibility(View.VISIBLE);
                mWebLayout.setVisibility(View.VISIBLE);
                mSurfaceLayout1.setVisibility(View.VISIBLE);
                mSurfaceLayout2.setVisibility(View.VISIBLE);
                mSurfaceLayout3.setVisibility(View.VISIBLE);
                mSurfaceLayout4.setVisibility(View.VISIBLE);
                mSurfaceLayout5.setVisibility(View.VISIBLE);
                mSurfaceViewMine.setVisibility(View.VISIBLE);
                mSurfaceViewTeacher.setVisibility(View.VISIBLE);
                mSurfaceView1.setVisibility(View.VISIBLE);
                mSurfaceView2.setVisibility(View.VISIBLE);
                mSurfaceView3.setVisibility(View.VISIBLE);
                mSurfaceView4.setVisibility(View.VISIBLE);
                mSurfaceView5.setVisibility(View.VISIBLE);


            }
        });

        Rx.click(mTimeBtn, 1000, v -> {//抢答
            mTimeBtn.setVisibility(View.GONE);
            HashMap<String, Object> data = new HashMap<>();
            data.put("username", ClassRoomManager.getInstance().getMySelf().nickName);

            HashMap<String, Object> params = new HashMap<>();
            params.put("id", "answer");
            params.put("name", "answer");
            params.put("toID", "__all");
            params.put("data", data);

            ClassRoomManager.getInstance().pubMsg(params);

        });

    }

    //===============白板========================

    //这个类中提供各种js可调用的方法。
    private class AndroidBridge {

        @JavascriptInterface
        public void sendBoardData(final String jsonStr) {
            System.out.println("jsonStrsend===========" + jsonStr);

            Gson gson = new Gson();
            HashMap message = new HashMap<>();
            message = gson.fromJson(jsonStr, message.getClass());

            String id = message.containsKey("id") ? message.get("id").toString() : "";
            String name = message.containsKey("signallingName") ? message.get("signallingName").toString() : "";
            String toID = message.containsKey("toID") ? message.get("toID").toString() : "";
            Object data = message.containsKey("data") ? message.get("data") : null;
            long seq = (message.containsKey("seq") && message.get("seq") instanceof Long) ? (Long) message.get("seq") : 0;
            long ts = (message.containsKey("ts") && message.get("ts") instanceof Long) ? (Long) message.get("ts") : 0;

            HashMap<String, Object> params = new HashMap<>();
            params.put("id", id);
            params.put("name", name);
            params.put("toID", toID);
            params.put("data", data);
            params.put("seq", seq);
            params.put("ts", ts);

            ClassRoomManager.getInstance().pubMsg(params);
        }

    }

    //===============媒体========================
    @Override
    public void roomManagerRoomJoined() {
        Log.e(TAG, "roomManagerRoomJoined");
//        JSONObject jsonObject = ClassRoomManager.getInstance().getRoomProperties();
//        try {
//            mTeacherName.setText(jsonObject.getString("firstname"));
//            teacherId = jsonObject.getString("userid");
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        mUserName.setText(LifeApplication.getInstance().getCurrentUser().getFirstname());


        if (ClassRoomManager.getInstance().isAudioEnabled()) {
            mSayImg.setBackgroundResource(R.mipmap.ic_class_voice_press);
        } else {
            mSayImg.setBackgroundResource(R.mipmap.ic_class_voice);
        }
        if (ClassRoomManager.getInstance().isVideoEnabled()) {
            mRenImg.setBackgroundResource(R.mipmap.ic_class_video_press);
        } else {
            mRenImg.setBackgroundResource(R.mipmap.ic_class_video);
        }
        mDrawImg.setBackgroundResource(R.mipmap.ic_class_draw);

        allRoomUserList = ClassRoomManager.getInstance().getRoomUsers();
        mAdapter = new CommentListAdapter(new ArrayList<>(0), teacherId,
                mTeacherName.getText().toString());
        mCommList.setLayoutManager(new LinearLayoutManager(this));
        mCommList.setAdapter(mAdapter);

        AllRoomUser.RoomUser nowUser = new AllRoomUser.RoomUser(AllRoomUser.RoomUser.MSG);
        nowUser.setReceiveid(ClassRoomManager.getInstance().getMySelf().peerId);
        nowUser.setFirstname(ClassRoomManager.getInstance().getMySelf().nickName);
        nowUser.setState(0);
        nowUser.setTime(getTime());
        recUserList.add(nowUser);
        mAdapter.setNewData(recUserList);
        mCommList.scrollToPosition(mAdapter.getItemCount() - 1);
    }

    @Override
    public void roomManagerDidFinish() {
        mTimeText.stop();
        if (bottomViews != null && bottomViews.size() > 0) {
            bottomViews.clear();
        }
        watchingWhom = null;
        this.finish();
    }

    @Override
    public void roomManagerUserJoined(ClassRoomUser user, boolean inList) {

        Log.e(TAG, "roomManagerUserJoined " + user.toString() + (inList ? " inlist" : ""));


        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (user.role == 0) {
                    mTeacherName.setText(user.nickName);
                    teacherId = user.peerId;
                }
            }
        });


        if (user.role == 0 && ClassRoomManager.getInstance().getMySelf().role == 0 ||
                (ClassRoomManager.getInstance().getRoomType() == 0 &&
                        user.role == ClassRoomManager.getInstance().getMySelf().role)) {
            ClassRoomManager.getInstance().evictUser(user.peerId);
            ClassRoomManager.getInstance().reShow();


        }

        showUserJoinedMessage(user);
    }

    private void showUserJoinedMessage(ClassRoomUser user) {
        boolean showComm = false;
        if (recUserList != null && recUserList.size() > 0) {
            for (int i = 0; i < recUserList.size(); i++) {
                if (user.peerId.equals(recUserList.get(i).getFirstname()) &&
                        String.valueOf(user.publishState).equals(String.valueOf(recUserList.get(i).getState()))) {
                    showComm = false;
                    break;
                } else {
                    showComm = true;
                }
            }
            if (showComm) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AllRoomUser.RoomUser nowUser = new AllRoomUser.RoomUser(AllRoomUser.RoomUser.MSG);
                        nowUser.setReceiveid(user.peerId);
                        nowUser.setFirstname(getOtherName(user.peerId));
                        nowUser.setState(user.publishState);
                        nowUser.setTime(getTime());
                        recUserList.add(nowUser);
                        mAdapter.setNewData(recUserList);
                        mCommList.scrollToPosition(mAdapter.getItemCount() - 1);
                    }
                });
            }
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    AllRoomUser.RoomUser nowUser = new AllRoomUser.RoomUser(AllRoomUser.RoomUser.MSG);
                    nowUser.setReceiveid(user.peerId);
                    nowUser.setFirstname(getOtherName(user.peerId));
                    nowUser.setState(user.publishState);
                    nowUser.setTime(getTime());
                    recUserList.add(nowUser);
                    if (mAdapter!=null&&recUserList != null && recUserList.size() > 0) {
                        mAdapter.setNewData(recUserList);
                        mCommList.scrollToPosition(mAdapter.getItemCount() - 1);
                    }
                }
            });
        }
    }

    private String getTime() {
        String time;
        Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
        t.setToNow(); // 取得系统时间。
        String hour = String.valueOf(t.hour);
        String minute;
        if (t.minute < 10) {
            minute = "0" + t.minute;
        } else {
            minute = String.valueOf(t.minute);
        }
        time = hour + ":" + minute;
        return time;
    }

    @Override
    public void roomManagerDidFailWithError(int error) {
        if (!isClassBegin) {
            ClassRoomManager.getInstance().set_status(0);
        } else {
            ClassRoomManager.getInstance().leaveRoom();
        }
        watchingWhom = null;
        this.finish();
    }


    @Override
    public void roomManagerUserLeft(ClassRoomUser user) {
        Log.e(TAG, "roomManagerUserLeft " + user.peerId);
        if (user.peerId.equals(teacherId) && isClassBegin) {
            watchingWhom = null;
            isClassBegin = false;
            mSurfaceViewTeacher.release();
            ClassRoomManager.getInstance().leaveRoom();
            Toast.makeText(this, "教师端app页面重启，将重新进入课堂", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            // 获取用户计算后的结果
            intent.putExtra("type", "open"); //将计算的值回传回去
            //通过intent对象返回结果，必须要调用一个setResult方法，
            //setResult(resultCode, data);第一个参数表示结果返回码，一般只要大于1就可以，但是
            setResult(2, intent);
            finish(); //结束当前的activity的生命周期
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    closeComaeMySelf(user.peerId);
                    AllRoomUser.RoomUser nowUser = new AllRoomUser.RoomUser(AllRoomUser.RoomUser.MSG);
                    nowUser.setReceiveid(user.peerId);
                    nowUser.setFirstname(getOtherName(user.peerId));
                    nowUser.setState(-1);
                    nowUser.setTime(getTime());
                    recUserList.add(nowUser);
                    mAdapter.setNewData(recUserList);
                    mCommList.scrollToPosition(mAdapter.getItemCount() - 1);
                }
            });
        }
    }

    @Override
    public void roomManagerUserChanged(ClassRoomUser user) {
        Log.e(TAG, "roomManagerUserChanged " + user.toString());
        if (user.peerId.equals(ClassRoomManager.getInstance().getMySelf().peerId)) {
            if (user.publishState > 0) {
                //启动摄像头
                boolean b = bottomViews.contains(user.peerId);
                if (!b) {
                    if (mSurfaceView1.getVisibility() == View.INVISIBLE) {
                        showUserMsg(mBottomLayout1, mOtherName1, mVideoImg1, mVoiceImg1, mDrawImg1,
                                mSurfaceView1, user);
                        bottomViews.add(0, user.peerId);
                    } else if (mSurfaceView2.getVisibility() == View.INVISIBLE) {
                        showUserMsg(mBottomLayout2, mOtherName2, mVideoImg2, mVoiceImg2, mDrawImg2,
                                mSurfaceView2, user);
                        bottomViews.add(1, user.peerId);
                    } else if (mSurfaceView3.getVisibility() == View.INVISIBLE) {
                        showUserMsg(mBottomLayout3, mOtherName3, mVideoImg3, mVoiceImg3, mDrawImg3,
                                mSurfaceView3, user);
                        bottomViews.add(2, user.peerId);
                    } else if (mSurfaceView4.getVisibility() == View.INVISIBLE) {
                        showUserMsg(mBottomLayout4, mOtherName4, mVideoImg4, mVoiceImg4, mDrawImg4,
                                mSurfaceView4, user);
                        bottomViews.add(3, user.peerId);
                    } else if (mSurfaceView5.getVisibility() == View.INVISIBLE) {
                        showUserMsg(mBottomLayout5, mOtherName5, mVideoImg5, mVoiceImg5, mDrawImg5,
                                mSurfaceView5, user);
                        bottomViews.add(4, user.peerId);
                    } else if (mSurfaceView6.getVisibility() == View.INVISIBLE) {
                        showUserMsg(mBottomLayout6, mOtherName6, mVideoImg6, mVoiceImg6, mDrawImg6,
                                mSurfaceView6, user);
                        bottomViews.add(5, user.peerId);
                    }
                } else {
                    changeUserMsg(user);
                }
            } else if (user.publishState == 0) {
                closeComaeMySelf(user.peerId);
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (!TextUtils.isEmpty(user.giftnumber)) {
                        mGiftNum.setText(user.giftnumber);
                    }
                    if (user.publishState == 0) {
                        mSayImg.setBackgroundResource(R.mipmap.ic_class_voice);
                        mRenImg.setBackgroundResource(R.mipmap.ic_class_video);
                        audioManager.setMicrophoneMute(true);
                    } else if (user.publishState == 1) {
                        mSayImg.setBackgroundResource(R.mipmap.ic_class_voice_press);
                        mRenImg.setBackgroundResource(R.mipmap.ic_class_video);
                        audioManager.setMicrophoneMute(false);
                    } else if (user.publishState == 2) {
                        mSayImg.setBackgroundResource(R.mipmap.ic_class_voice);
                        mRenImg.setBackgroundResource(R.mipmap.ic_class_video_press);
                        audioManager.setMicrophoneMute(true);
                    } else if (user.publishState == 3) {
                        mSayImg.setBackgroundResource(R.mipmap.ic_class_voice_press);
                        mRenImg.setBackgroundResource(R.mipmap.ic_class_video_press);
                        audioManager.setMicrophoneMute(false);
                    }
                    if (user.canDraw) {
                        mDrawImg.setBackgroundResource(R.mipmap.ic_class_draw_press);
                        mShowView.loadUrl("javascript:GLOBAL.phone.drawPermission(true)");
                    } else {
                        mDrawImg.setBackgroundResource(R.mipmap.ic_class_draw);
                        mShowView.loadUrl("javascript:GLOBAL.phone.drawPermission(false)");
                    }
                }
            });
        } else {
            changeUserMsg(user);
        }
    }

    /**
     * 改变上台人员信息
     *
     * @param user
     */
    private void changeUserMsg(ClassRoomUser user) {
        Log.e(TAG, "changeUserMsg: " + user.toString());
        if (bottomViews != null && bottomViews.size() > 0 && bottomViews.contains(user.peerId)) {
            for (int i = 0; i < bottomViews.size(); i++) {
                if (user.peerId.equals(bottomViews.get(i))) {
                    int item = i + 1;
                    switch (item) {
                        case 1:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (user.publishState == 0) {
                                        mVoiceImg1.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg1.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 1) {
                                        mVoiceImg1.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg1.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 2) {
                                        mVoiceImg1.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg1.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    } else if (user.publishState == 3) {
                                        mVoiceImg1.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg1.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    }
                                    if (user.canDraw) {
                                        mDrawImg1.setBackgroundResource(R.mipmap.ic_class_draw_press);
                                    } else {
                                        mDrawImg1.setBackgroundResource(R.mipmap.ic_class_draw);
                                    }
                                }
                            });
                            break;
                        case 2:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (user.publishState == 0) {
                                        mVoiceImg2.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg2.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 1) {
                                        mVoiceImg2.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg2.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 2) {
                                        mVoiceImg2.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg2.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    } else if (user.publishState == 3) {
                                        mVoiceImg2.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg2.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    }
                                    if (user.canDraw) {
                                        mDrawImg2.setBackgroundResource(R.mipmap.ic_class_draw_press);
                                    } else {
                                        mDrawImg2.setBackgroundResource(R.mipmap.ic_class_draw);
                                    }
                                }
                            });
                            break;
                        case 3:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (user.publishState == 0) {
                                        mVoiceImg3.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg3.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 1) {
                                        mVoiceImg3.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg3.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 2) {
                                        mVoiceImg3.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg3.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    } else if (user.publishState == 3) {
                                        mVoiceImg3.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg3.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    }
                                    if (user.canDraw) {
                                        mDrawImg3.setBackgroundResource(R.mipmap.ic_class_draw_press);
                                    } else {
                                        mDrawImg3.setBackgroundResource(R.mipmap.ic_class_draw);
                                    }
                                }
                            });
                            break;
                        case 4:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (user.publishState == 0) {
                                        mVoiceImg4.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg4.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 1) {
                                        mVoiceImg4.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg4.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 2) {
                                        mVoiceImg4.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg4.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    } else if (user.publishState == 3) {
                                        mVoiceImg4.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg4.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    }
                                    if (user.canDraw) {
                                        mDrawImg4.setBackgroundResource(R.mipmap.ic_class_draw_press);
                                    } else {
                                        mDrawImg4.setBackgroundResource(R.mipmap.ic_class_draw);
                                    }
                                }
                            });
                            break;
                        case 5:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (user.publishState == 0) {
                                        mVoiceImg5.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg5.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 1) {
                                        mVoiceImg5.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg5.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 2) {
                                        mVoiceImg5.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg5.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    } else if (user.publishState == 3) {
                                        mVoiceImg5.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg5.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    }
                                    if (user.canDraw) {
                                        mDrawImg5.setBackgroundResource(R.mipmap.ic_class_draw_press);
                                    } else {
                                        mDrawImg5.setBackgroundResource(R.mipmap.ic_class_draw);
                                    }
                                }
                            });
                            break;
                        case 6:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (user.publishState == 0) {
                                        mVoiceImg6.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg6.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 1) {
                                        mVoiceImg6.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg6.setBackgroundResource(R.mipmap.ic_class_video);
                                    } else if (user.publishState == 2) {
                                        mVoiceImg6.setBackgroundResource(R.mipmap.ic_class_voice);
                                        mVideoImg6.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    } else if (user.publishState == 3) {
                                        mVoiceImg6.setBackgroundResource(R.mipmap.ic_class_voice_press);
                                        mVideoImg6.setBackgroundResource(R.mipmap.ic_class_video_press);
                                    }
                                    if (user.canDraw) {
                                        mDrawImg6.setBackgroundResource(R.mipmap.ic_class_draw_press);
                                    } else {
                                        mDrawImg6.setBackgroundResource(R.mipmap.ic_class_draw);
                                    }
                                }
                            });
                            break;
                    }
                }
            }
        }
    }

    /**
     * 显示上台人员信息
     *
     * @param mBottomLayout
     * @param mOtherName
     * @param mVideoImg
     * @param mVoiceImg
     * @param mDrawImg
     * @param mSurfaceView
     * @param user
     */
    private void showUserMsg(LinearLayout mBottomLayout, TextView mOtherName, ImageView mVideoImg,
                             ImageView mVoiceImg, ImageView mDrawImg, @NonNull SurfaceViewRenderer mSurfaceView,
                             ClassRoomUser user) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBottomLayout.setVisibility(View.VISIBLE);
                mOtherName.setText(getOtherName(user.peerId));
                if (user.publishState == 0) {
                    mVoiceImg.setBackgroundResource(R.mipmap.ic_class_voice);
                    mVideoImg.setBackgroundResource(R.mipmap.ic_class_video);
                } else if (user.publishState == 1) {
                    mVoiceImg.setBackgroundResource(R.mipmap.ic_class_voice_press);
                    mVideoImg.setBackgroundResource(R.mipmap.ic_class_video);
                } else if (user.publishState == 2) {
                    mVoiceImg.setBackgroundResource(R.mipmap.ic_class_voice);
                    mVideoImg.setBackgroundResource(R.mipmap.ic_class_video_press);
                } else if (user.publishState == 3) {
                    mVoiceImg.setBackgroundResource(R.mipmap.ic_class_voice_press);
                    mVideoImg.setBackgroundResource(R.mipmap.ic_class_video_press);
                }
                if (user.canDraw) {
                    mDrawImg.setBackgroundResource(R.mipmap.ic_class_draw_press);
                } else {
                    mDrawImg.setBackgroundResource(R.mipmap.ic_class_draw);
                }
                mSurfaceView.setVisibility(View.VISIBLE);
                mSurfaceView.init(EglBase.create().getEglBaseContext(), null);
                ClassRoomManager.getInstance().startMedia(mSurfaceView);
            }
        });
    }

    private void closeComaeMySelf(String peerId) {
        if (bottomViews != null && bottomViews.size() > 0 && bottomViews.contains(peerId)) {
            for (int i = 0; i < bottomViews.size(); i++) {
                if (peerId.equals(bottomViews.get(i))) {
                    int item = i + 1;
                    switch (item) {
                        case 1:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mBottomLayout1.setVisibility(View.GONE);
                                    mSurfaceView1.setVisibility(View.INVISIBLE);
                                    mSurfaceView1.release();
                                }
                            });
                            break;
                        case 2:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mBottomLayout2.setVisibility(View.GONE);
                                    mSurfaceView2.setVisibility(View.INVISIBLE);
                                    mSurfaceView2.release();
                                }
                            });

                            break;
                        case 3:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mBottomLayout3.setVisibility(View.GONE);
                                    mSurfaceView3.setVisibility(View.INVISIBLE);
                                    mSurfaceView3.release();
                                }
                            });
                            break;
                        case 4:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mBottomLayout4.setVisibility(View.GONE);
                                    mSurfaceView4.setVisibility(View.INVISIBLE);
                                    mSurfaceView4.release();
                                }
                            });
                            break;
                        case 5:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mBottomLayout5.setVisibility(View.GONE);
                                    mSurfaceView5.setVisibility(View.INVISIBLE);
                                    mSurfaceView5.release();
                                }
                            });
                            break;
                        case 6:
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mBottomLayout6.setVisibility(View.GONE);
                                    mSurfaceView6.setVisibility(View.INVISIBLE);
                                    mSurfaceView6.release();
                                }
                            });
                            break;
                    }
                }
            }
            bottomViews.remove(peerId);
        }
    }

    @Override
    public Object roomManagerUserVideoPublished(String peerId) {
        Log.e(TAG, "roomManagerUserVideoPublished " + peerId);
        if (peerId.equals(teacherId)) {
            if (watchingWhom == null) {
                watchingWhom = peerId;
                mSurfaceViewTeacher.init(EglBase.create().getEglBaseContext(), null);
                return mSurfaceViewTeacher;
            }
        } else if (!peerId.equals(watchingWhom)) {
            boolean b = bottomViews.contains(peerId);
            if (!b) {
                if (mSurfaceView1.getVisibility() == View.INVISIBLE) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBottomLayout1.setVisibility(View.VISIBLE);
                            mBottomLayout1.setAlpha(0.4f);
                            mOtherName1.setText(getOtherName(peerId));
                            mSurfaceView1.setVisibility(View.VISIBLE);
                            mSurfaceView1.init(EglBase.create().getEglBaseContext(), null);
                        }
                    });
                    bottomViews.add(0, peerId);
                    return mSurfaceView1;
                } else if (mSurfaceView2.getVisibility() == View.INVISIBLE) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBottomLayout2.setVisibility(View.VISIBLE);
                            mBottomLayout2.setAlpha(0.4f);
                            mOtherName2.setText(getOtherName(peerId));
                            mSurfaceView2.setVisibility(View.VISIBLE);
                            mSurfaceView2.init(EglBase.create().getEglBaseContext(), null);
                        }
                    });
                    bottomViews.add(1, peerId);
                    return mSurfaceView2;
                } else if (mSurfaceView3.getVisibility() == View.INVISIBLE) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBottomLayout3.setVisibility(View.VISIBLE);
                            mBottomLayout3.setAlpha(0.4f);
                            mOtherName3.setText(getOtherName(peerId));
                            mSurfaceView3.setVisibility(View.VISIBLE);
                            mSurfaceView3.init(EglBase.create().getEglBaseContext(), null);
                        }
                    });
                    bottomViews.add(2, peerId);
                    return mSurfaceView3;
                } else if (mSurfaceView4.getVisibility() == View.INVISIBLE) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBottomLayout4.setVisibility(View.VISIBLE);
                            mBottomLayout4.setAlpha(0.4f);
                            mOtherName4.setText(getOtherName(peerId));
                            mSurfaceView4.setVisibility(View.VISIBLE);
                            mSurfaceView4.init(EglBase.create().getEglBaseContext(), null);
                        }
                    });
                    bottomViews.add(3, peerId);
                    return mSurfaceView4;
                } else if (mSurfaceView5.getVisibility() == View.INVISIBLE) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBottomLayout5.setVisibility(View.VISIBLE);
                            mBottomLayout5.setAlpha(0.4f);
                            mOtherName5.setText(getOtherName(peerId));
                            mSurfaceView5.setVisibility(View.VISIBLE);
                            mSurfaceView5.init(EglBase.create().getEglBaseContext(), null);
                        }
                    });
                    bottomViews.add(4, peerId);
                    return mSurfaceView5;
                } else if (mSurfaceView6.getVisibility() == View.INVISIBLE) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBottomLayout6.setVisibility(View.VISIBLE);
                            mBottomLayout6.setAlpha(0.4f);
                            mOtherName6.setText(getOtherName(peerId));
                            mSurfaceView6.setVisibility(View.VISIBLE);
                            mSurfaceView6.init(EglBase.create().getEglBaseContext(), null);
                        }
                    });
                    bottomViews.add(5, peerId);
                    return mSurfaceView6;
                }
            }
        }
        return null;
    }

    private String getOtherName(String peerId) {
        String name;
        for (int i = 0; i < allRoomUserList.size(); i++) {
            if (peerId.equals(allRoomUserList.get(i).getReceiveid())) {
                name = allRoomUserList.get(i).getAccount();
                return name;
            }
        }
        return null;
    }

    @Override
    public void roomManagerUserVideoUnPublished(String peerId) {
        Log.d(TAG, "roomManagerUserVideoUnPublished " + peerId);
        if (watchingWhom != null && watchingWhom.equals(peerId)) {
            watchingWhom = null;
            mSurfaceViewTeacher.release();
        } else {
            closeComaeMySelf(peerId);
        }

    }

    @Override
    public void roomManagerIceStatusChanged(String peerId, String state) {
        Log.e(TAG, "roomManagerIceStatusChanged " + state + " " + peerId);
    }

    @Override
    public void roomManagerMessageReceived(String peerId, String message) {
        Log.e(TAG, "roomManagerMessageReceived " + peerId);


        if (!TextUtils.isEmpty(peerId) && allRoomUserList != null && allRoomUserList.size() > 0) {
            Log.e(TAG, "roomManagerMessageReceived:allRoomUserList" + allRoomUserList.toString());
            String receivedName = null;
            for (int i = 0; i < allRoomUserList.size(); i++) {
                Log.e(TAG, "roomManagerMessageReceived:name " + allRoomUserList.get(i).getAccount());
                if (peerId.equals(allRoomUserList.get(i).getReceiveid())) {
                    receivedName = allRoomUserList.get(i).getAccount();
                    break;
                } else if (peerId.equals(teacherId)) {
                    receivedName = mTeacherName.getText().toString().trim();
                    break;
                } else if (peerId.equals(LifeApplication.getInstance().getCurrentUser().getUserid())) {
                    receivedName = LifeApplication.getInstance().getCurrentUser().getAccount();
                    break;
                }
            }
            if (!TextUtils.isEmpty(receivedName)) {
                String finalReceivedName = receivedName;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AllRoomUser.RoomUser user = new AllRoomUser.RoomUser(AllRoomUser.RoomUser.COM);
                        user.setFirstname(finalReceivedName);
                        user.setReceiveid(peerId);
                        user.setMessage(message);
                        user.setTime(getTime());
                        user.setState(-1);
                        recUserList.add(user);
                        mAdapter.setNewData(recUserList);
                        mCommList.scrollToPosition(mAdapter.getItemCount() - 1);
                        mSayEdt.setText("");
                    }
                });

            }
        }
    }

    @Override
    public void roomManagerOnRemoteMsg(boolean add, String id, String name, String fromWhom, long ts, Object data) {
        Log.e(TAG, "roomManagerOnRemoteMsg " + add + " ts:" + ts + "name:" + name + "  data: " + data);
        if (name.equals("ClassBegin")) {
            if (add) { // class begins
                Log.e(TAG, "class begins");
                dismissLoadDialog();


                isClassBegin = true;
                classStarTime = ts;
                ClassRoomManager.getInstance().pubMsg("UpdateTime", "UpdateTime",
                        ClassRoomManager.getInstance().getMySelf().peerId, null);
                if (roomType.equals("0"))
                    ClassRoomManager.getInstance().changeUserPublish(ClassRoomManager.getInstance().getMySelf().peerId, 3);
//                if (needJoin) {
//                    needJoin = false;
//                    afterPageFinished();
//                }
                Gson gson = new Gson();
                HashMap message = new HashMap<>();
                message = gson.fromJson(data.toString(), message.getClass());
                Map mediaProgress1 = message.containsKey("MediaProgress_video_1") ? (Map) message.get("MediaProgress_video_1") : null;
                Map mediaProgress0 = message.containsKey("MediaProgress_video_0") ? (Map) message.get("MediaProgress_video_0") : null;
                Map mediaProgressShowPage = message.containsKey("Video_MediaFilePage_ShowPage") ? (Map) message.get("Video_MediaFilePage_ShowPage") : null;
                Map audioProgressShowPage = message.containsKey("Audio_MediaFilePage_ShowPage") ? (Map) message.get("Audio_MediaFilePage_ShowPage") : null;
                if (mediaProgress0 != null) {
                    Map audioProgressShowPageDetail = null;
                    Map mediaProgressShowPageDetail = null;
                    Map mediaProgress1Detail = null;
                    Map mediaProgress0Detail = (Map) mediaProgress0.get("data");
                    if (mediaProgressShowPage != null) {
                        mediaProgressShowPageDetail = (Map) mediaProgressShowPage.get("data");
                    }
                    if (mediaProgress1 != null) {
                        mediaProgress1Detail = (Map) mediaProgress1.get("data");
                    }
                    if (audioProgressShowPage != null) {
                        audioProgressShowPageDetail = (Map) audioProgressShowPage.get("data");
                    }
                    Map fileDate = null;
                    if (mediaProgressShowPageDetail != null) {
                        fileDate = mediaProgressShowPageDetail.containsKey("filedata") ? (Map) mediaProgressShowPageDetail.get("filedata") : null;
                    } else if (audioProgressShowPageDetail != null) {
                        fileDate = audioProgressShowPageDetail.containsKey("filedata") ? (Map) audioProgressShowPageDetail.get("filedata") : null;
                    }
                    if (fileDate != null) {
                        type = (String) fileDate.get("filetype");
                        String path = (String) fileDate.get("swfpath");
                        filename = (String) fileDate.get("filename");
                        if (!TextUtils.isEmpty(path) && path.contains(".")) {
                            String[] strarray = path.split("\\.");
//                            path = "http://edu.weiyicloud.com" + strarray[0] + "-1." + strarray[1];
                            path = "http://www.weiyiclass.com" + strarray[0] + "-1." + strarray[1];
                        }
                        Uri uri = Uri.parse(path);
                        Log.d("Uri=======", uri.toString());
                        if (type.equals("mp4") || type.equals("webm")) {
                            type = "video";
                        } else if (type.equals("mp3") || type.equals("wav") || type.equals("ogg")) {
                            type = "audio";
                        }
                        open(uri, type);//打开视频
                    }
                    boolean play = true;
                    if (mediaProgress0Detail != null) {
                        Object playType = mediaProgress0Detail.containsKey("play") ? mediaProgress0Detail.get("play") : null;
                        Object currentTime = mediaProgress0Detail.containsKey("currentTime") ? mediaProgress0Detail.get("currentTime") : null;
                        type = mediaProgress0Detail.containsKey("mediaType") ? (String) mediaProgress0Detail.get("mediaType") : "";
                        int time = 0;
                        if (currentTime != null) {
                            String timeStr = currentTime.toString();
                            if (timeStr.contains(".")) {
                                String[] strarray = timeStr.split("\\.");
                                time = Integer.valueOf(strarray[0]);
                            }
                        }
                        time = time * 1000;
                        String dataType = "";
                        if (playType != null) {
                            if (playType instanceof String) {
                                dataType = (String) playType;
                            } else if (playType instanceof Boolean) {
                                play = (Boolean) playType;
                            }
                            if ("1".equals(dataType)) {
                                play = true;
                            }
                        }
                        if (play) {
                            pause(time);
                        } else {
                            suspend();
                        }
                    }
                    if (mediaProgress1Detail != null) {
                        Object currentTime = mediaProgress1Detail.containsKey("currentTime") ? mediaProgress1Detail.get("currentTime") : null;
                        type = mediaProgress1Detail.containsKey("mediaType") ? (String) mediaProgress1Detail.get("mediaType") : "";
                        int time = 0;
                        if (currentTime != null) {
                            String timeStr = currentTime.toString();
                            if (timeStr.contains(".")) {
                                String[] strarray = timeStr.split("\\.");
                                time = Integer.valueOf(strarray[0]);
                            } else {
                                time = Integer.valueOf(timeStr);
                            }
                        }
                        time = time * 1000;
                        if (play) {
                            pause(time);
                        } else {
                            suspend();
                        }
                    }
                }
            } else { // class end
                Log.e(TAG, "class ends");
                isClassBegin = false;
                mTimeText.stop();
//                ClassRoomManager.getInstance().changeUserPublish(ClassRoomManager.getInstance().getMySelf().peerId, 0);
            }
        } else if (name.equals("UpdateTime")) {
            if (add) {
                if (isClassBegin) {
                    if (seveiceTime != ts) {
                        seveiceTime = ts;
                        localTime = seveiceTime - classStarTime;
                        Log.e(TAG, "localTime====" + localTime);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //设置初始值（重置）
                                mTimeText.setBase(SystemClock.elapsedRealtime() - localTime * 1000);//计时器清零
                                int hour = (int) ((SystemClock.elapsedRealtime() - mTimeText.getBase()) / 1000 / 3600);
                                if (hour > 0) {
                                    mTimeText.setFormat("0" + "%s");
                                } else {
                                    mTimeText.setFormat("0" + String.valueOf(hour) + ":%s");
                                }
                                mTimeText.start();
                            }
                        });
                    }
                }
            }
        } else if (name.equals("playDice")) {//掷骰子
            if (add) {
                Gson gson = new Gson();
                HashMap message = new HashMap<>();
                message = gson.fromJson(data.toString(), message.getClass());
                Object isShowDice = message.containsKey("isShowDice") ? message.get("isShowDice") : null;
                ArrayList<Object> playList = message.containsKey("stus") ? (ArrayList<Object>) message.get("stus") : null;
                String dataType = "";
                boolean isShowDiceType = false;
                if (isShowDice != null) {
                    if (isShowDice instanceof String) {
                        dataType = isShowDice.toString();
                    } else if (isShowDice instanceof Boolean) {
                        isShowDiceType = (Boolean) isShowDice;
                    }
                    if ("1".equals(dataType)) {
                        isShowDiceType = true;
                    }
                }
                if (isShowDiceType && playList != null) {//开始摇色子
                    showPlayDice(playList);
                } else {//停止摇骰子
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mPlayImg1.setVisibility(View.GONE);
                            mPlayImg2.setVisibility(View.GONE);
                            mPlayImg3.setVisibility(View.GONE);
                            mPlayImg4.setVisibility(View.GONE);
                            mPlayImg5.setVisibility(View.GONE);
                            mPlayImg6.setVisibility(View.GONE);
                        }
                    });

                }
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mPlayImg1.setVisibility(View.GONE);
                        mPlayImg2.setVisibility(View.GONE);
                        mPlayImg3.setVisibility(View.GONE);
                        mPlayImg4.setVisibility(View.GONE);
                        mPlayImg5.setVisibility(View.GONE);
                        mPlayImg6.setVisibility(View.GONE);
                    }
                });
            }
        } else if (name.equals("startAnswer")) {//开始抢答
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mAnswerLayout.setVisibility(View.VISIBLE);
                    mTimeShow.setVisibility(View.VISIBLE);
                    mTimeBtn.setVisibility(View.GONE);
                    timer.start();
                }
            });

        } else if (name.equals("stopAnswer")) {//抢答结束
            showMessage("抢答结束，本轮无人抢答");
        } else if (name.equals("answer")) {//有人已抢答
            Gson gson = new Gson();
            HashMap message = new HashMap<>();
            message = gson.fromJson(data.toString(), message.getClass());
            String username = message.containsKey("username") ? message.get("username").toString() : "";
            showMessage("优胜者为:".concat(username));
        } else if (name.equals("ShowPage")) {
            if (add) {
                Gson gson = new Gson();
                HashMap message = new HashMap<>();
                message = gson.fromJson(data.toString(), message.getClass());
                Object isMedia = message.containsKey("ismedia") ? message.get("ismedia") : null;
                Map video = message.containsKey("filedata") ? (Map) message.get("filedata") : null;
                boolean isMediaType = false;
                String dataType = "";
                if (isMedia != null) {
                    if (isMedia instanceof String) {
                        dataType = (String) isMedia;
                    } else if (isMedia instanceof Boolean) {
                        isMediaType = (Boolean) isMedia;
                    }
                    if ("1".equals(dataType)) {
                        isMediaType = true;
                    }
                }
                if (isMediaType && video != null) {
                    oldID = id;
                    String path = video.get("swfpath").toString();
                    type = video.get("filetype").toString();
                    filename = video.get("filename").toString();
                    if (!TextUtils.isEmpty(path) && path.contains(".")) {
                        String[] strarray = path.split("\\.");
//                        path = "http://edu.weiyicloud.com" + strarray[0] + "-1." + strarray[1];
                        path = "http://www.weiyiclass.com" + strarray[0] + "-1." + strarray[1];
                    }
                    Uri uri = Uri.parse(path);
                    Log.d("Uri=======", uri.toString());
                    if (type.equals("mp4") || type.equals("webm")) {
                        type = "video";
                    } else if (type.equals("mp3") || type.equals("wav") || type.equals("ogg")) {
                        type = "audio";
                    }
                    open(uri, type);//打开视频
                }
            } else if (!add && id.equals(oldID)) {
                stop();//关闭视频
            }
        } else if (name.equals("MediaProgress")) {
            Gson gson = new Gson();
            HashMap message = new HashMap<>();
            message = gson.fromJson(data.toString(), message.getClass());

            Object actionType = message.containsKey("action") ? message.get("action") : null;
            Object currentTime = message.containsKey("currentTime") ? message.get("currentTime") : null;
            Object playType = message.containsKey("play") ? message.get("play") : null;
            type = message.containsKey("mediaType") ? (String) message.get("mediaType") : "";
            boolean play = false;
            String dataType = "";
            if (playType != null) {
                if (playType instanceof String) {
                    dataType = playType.toString();
                } else if (playType instanceof Boolean) {
                    play = (Boolean) playType;
                }
                if ("1".equals(dataType)) {
                    play = true;
                }
            }
            int time = 0;
            if (currentTime != null) {
                String timeStr = currentTime.toString();
                if (timeStr.contains(".")) {
                    String[] strarray = timeStr.split("\\.");
                    time = Integer.valueOf(strarray[0]);
                } else {
                    time = Integer.valueOf(timeStr);
                }
            }
            time = time * 1000;
            int AnctionType = 0;
            if (actionType != null) {
                String action = actionType.toString();
                if (action.contains(".")) {
                    String[] strarray = action.split("\\.");
                    AnctionType = Integer.valueOf(strarray[0]);
                } else {
                    AnctionType = Integer.valueOf(action);
                }
            }
            if (0 == AnctionType) {
                if (play) {
                    pause(time);
                } else {
                    suspend();
                }
            } else if (1 == AnctionType) {
                int finalTime = time;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //按照初始位置播放
                        mVideoView.seekTo(finalTime);
                        startNewThread();
                    }
                });
            }

        } else if (TextUtils.isEmpty(name)) {
            isClassBegin = false;
            needJoin = true;
            watchingWhom = null;
            dismissLoadDialog();
            return;
        }
        dismissLoadDialog();
        if (data != null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if ("0".equals(fromWhom)) {
                        Log.d(TAG, "roomManagerOnRemoteMsgUrl: " + "javascript:GLOBAL.phone.receivePhoneByTriggerEvent(" + "\"message-list-received\"" + ", " + data + ")");
                        mShowView.loadUrl("javascript:GLOBAL.phone.receivePhoneByTriggerEvent(" + "\"message-list-received\"" + ", " + data + ")");
                    } else if ("1".equals(fromWhom)) {
                        JSONObject nowData = new JSONObject();
                        if (add) {
                            try {
                                nowData.put("id", id);
                                nowData.put("name", name);
                                nowData.put("ts", ts);
                                nowData.put("data", data);
                                if (nowData.getString("data") != null) {
                                    Log.d(TAG, "roomManagerOnRemoteMsgUrl: " + "javascript:GLOBAL.phone.receivePhoneByTriggerEvent(" + "\"publish-message-received\"" + ", " + nowData + ")");
                                    mShowView.loadUrl("javascript:GLOBAL.phone.receivePhoneByTriggerEvent(" + "\"publish-message-received\"" + ", " + nowData + ")");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Log.d(TAG, "roomManagerOnRemoteMsgUrl: " + "javascript:GLOBAL.phone.receivePhoneByTriggerEvent(" + "\"delete-message-received\"" + ", " + data + ")");
                            mShowView.loadUrl("javascript:GLOBAL.phone.receivePhoneByTriggerEvent(" + "\"delete-message-received\"" + ", " + data + ")");
                        }

                    }
                }
            });
        }
    }

    private void showPlayDice(ArrayList<Object> playList) {

        if (bottomViews != null && bottomViews.size() > 0) {
            for (int i = 0; i < playList.size(); i++) {
                Map<String, Object> user = (Map<String, Object>) playList.get(i);
                String id = user.containsKey("id") ? user.get("id").toString() : "";
                for (int j = 0; j < bottomViews.size(); j++) {
                    if (id.equals(bottomViews.get(j))) {
                        int count = j + 1;
                        String num = user.containsKey("num") ? user.get("num").toString() : "0";
                        switch (count) {
                            case 1:
                                showView(mPlayImg1, num);
                                break;
                            case 2:
                                showView(mPlayImg2, num);
                                break;
                            case 3:
                                showView(mPlayImg3, num);
                                break;
                            case 4:
                                showView(mPlayImg4, num);
                                break;
                            case 5:
                                showView(mPlayImg5, num);
                                break;
                            case 6:
                                showView(mPlayImg6, num);
                                break;
                        }
                    }
                }
            }

        }
    }

    private void open(Uri uri, String type) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mVideoShowLayout.setVisibility(View.VISIBLE);
                if (type.equals("audio")) {
                    if (mVideoView != null) {
                        mVideoView.stopPlayback();
                    }
                    if (player != null) {
                        player.stop();
                    }
                    mVideoView.setVisibility(View.GONE);
                    mEnlargeBtn.setVisibility(View.GONE);
                    mAudioName.setVisibility(View.VISIBLE);
                    mAudioName.setText(filename);
                    player = MediaPlayer.create(getContext(), uri);
                    player.start();
                    mSeekBar.setMax(player.getDuration());
                    isPlaying = true;
                } else {
                    if (player != null) {
                        player.stop();
                    }
                    mVideoView.setVisibility(View.VISIBLE);
                    mEnlargeBtn.setVisibility(View.VISIBLE);
                    mAudioName.setVisibility(View.GONE);
                    //设置视频路径
                    mVideoView.setVideoURI(uri);
                    //开始播放视频
                    mVideoView.start();
                    // 按照初始位置播放
                    mVideoView.seekTo(0);
                    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            // 设置进度条的最大进度为视频流的最大播放时长
                            isPlaying = true;
                            mSeekBar.setMax(mVideoView.getDuration());
                        }
                    });
                    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            // 在播放完毕被回调
                            mSeekBar.setProgress(0);
                        }
                    });
                    mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                        @Override
                        public boolean onError(MediaPlayer mp, int what, int extra) {
                            return false;
                        }
                    });
                }
                startNewThread();
            }
        });
    }

    private void startNewThread() {
        new Thread() {
            @Override
            public void run() {
                try {
                    while (isPlaying) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // 如果正在播放，每秒更新一次进度条
                                int current = 0;
                                if ("audio".equals(type)) {
                                    current = player.getCurrentPosition();
                                } else {
                                    current = mVideoView.getCurrentPosition();
                                }
                                mSeekBar.setProgress(current);
                                mTimeChangeTV.setText(stringForTime(mSeekBar.getProgress()) + "/" + stringForTime(mSeekBar.getMax()));
                            }
                        });
                        sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    //进度转时间
    private String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

    /**
     * 暂停播放
     */
    private void suspend() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isPlaying) {
                    isPlaying = false;
                    isStop = true;
                    if ("audio".equals(type)) {
                        player.pause();
                    } else {
                        mVideoView.pause();
                    }
                    Toast.makeText(getContext(), "暂停播放", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 继续播放
     */
    private void pause(int time) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isStop) {
                    isStop = false;
                    isPlaying = true;
                    if ("audio".equals(type)) {
                        if (player != null) {
                            player.start();
                            player.seekTo(time);
                        }
                    } else {
                        mVideoView.start();
                        mVideoView.seekTo(time);
                    }

                    Toast.makeText(getContext(), "继续播放", Toast.LENGTH_SHORT).show();
                    startNewThread();
                }
            }
        });
    }

    private void stop() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                isPlaying = false;
                if ("audio".equals(type)) {
                    if (player != null) {
                        player.stop();
                    }
                } else if (mVideoView != null && mVideoView.isPlaying()) {
                    mVideoView.stopPlayback();
                }
                mVideoShowLayout.setVisibility(View.GONE);
            }
        });

    }

    private void showView(ImageView mPlayImg, String num) {
        Log.e(TAG, "showView: num==========" + num);
        int number = 0;
        if (num.contains(".")) {
            int idx = num.lastIndexOf(".");//查找小数点的位置
            String strNum = num.substring(0, idx);//截取从字符串开始到小数点位置的字符串，就是整数部分
            number = Integer.valueOf(strNum);//把整数部分通过Integer.valueof方法转换为数字
        } else {
            number = Integer.valueOf(num);
        }
        int finalNumber = number;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mPlayImg.setVisibility(View.VISIBLE);
                AnimationDrawable frameAnim = (AnimationDrawable) getResources().getDrawable(R.drawable.bullet_anim);
                mPlayImg.setBackgroundDrawable(frameAnim);
                frameAnim.start();
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        //execute the task
                        frameAnim.stop();
                        switch (finalNumber) {//显示筛子数字
                            case 1:
                                mPlayImg.setBackgroundResource(R.mipmap.ic_one);
                                break;
                            case 2:
                                mPlayImg.setBackgroundResource(R.mipmap.ic_two);
                                break;
                            case 3:
                                mPlayImg.setBackgroundResource(R.mipmap.ic_three);
                                break;
                            case 4:
                                mPlayImg.setBackgroundResource(R.mipmap.ic_four);
                                break;
                            case 5:
                                mPlayImg.setBackgroundResource(R.mipmap.ic_five);
                                break;
                            case 6:
                                mPlayImg.setBackgroundResource(R.mipmap.ic_six);
                                break;
                        }
                    }
                }, 3000);
            }
        });

    }

    private void showMessage(String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mAnswerLayout.setVisibility(View.VISIBLE);
                mTimeShow.setVisibility(View.GONE);
                mTimeBtn.setVisibility(View.GONE);
                mWinner.setVisibility(View.VISIBLE);
                mWinner.setText(message);
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        //execute the task
                        mAnswerLayout.setVisibility(View.GONE);
                        mWinner.setVisibility(View.GONE);
                    }
                }, 2000);
            }
        });
    }

    private CountDownTimer timer = new CountDownTimer(4000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            mTimeShow.setText(String.valueOf(millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            mTimeShow.setVisibility(View.GONE);
            mTimeBtn.setVisibility(View.VISIBLE);
        }
    };

    @Override
    public void roomGiftInfo(String num) {
        mGiftNum.setText(num);
    }

    @Override
    public void roomFileInfo(List<RoomFile> list) {

    }

    @Override
    public void participantEvicted(ClassRoomUser myself) {
        Log.e(TAG, "participantEvicted: " + myself.toString());
        if (myself.peerId.equals(LifeApplication.getInstance().getCurrentUser().getUserid()) && isClassBegin) {
            watchingWhom = null;
            ClassRoomManager.getInstance().leaveRoom();
            showClosedDialog();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isClassBegin) {
                ClassRoomManager.getInstance().set_status(0);
                watchingWhom = null;
                finish();
            } else {
                watchingWhom = null;
                showLeaveDialog();
            }
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private void showLeaveDialog() {
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setTitle("退出课堂");
        normalDialog.setMessage("课程进行中，是否要退出课堂?");
        normalDialog.setPositiveButton("是", (dialog, which) -> {
            dialog.dismiss();
            ClassRoomManager.getInstance().leaveRoom();
            finish();
        });
        normalDialog.setNegativeButton("否", (dialog, which) -> dialog.dismiss());
        // 显示
        normalDialog.show();
    }

    private void showClosedDialog() {
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setTitle("退出课堂");
        normalDialog.setMessage("有相同账号进入课堂，您将退出程序");
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton("确定", (dialog, which) -> {
            dialog.dismiss();
            finish();
            LifeApplication.getInstance().systemExit();
        });
        normalDialog.show();
    }

}
